package com.koron.car.web.authorized.service.impl;

import com.alibaba.fastjson.JSON;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.bean.workflow.RedirectQuery;
import com.koron.car.config.Constants;
import com.koron.car.web.authorized.bean.entity.CarDelimit;
import com.koron.car.web.authorized.bean.entity.CarReplace;
import com.koron.car.web.authorized.bean.query.ReplaceDelimitQuery;
import com.koron.car.web.authorized.bean.vo.CarReplaceVO;
import com.koron.car.web.authorized.bean.vo.ReplaceDelimitVO;
import com.koron.car.web.authorized.mapper.CarDelimitMapper;
import com.koron.car.web.authorized.mapper.CarReplaceMapper;
import com.koron.car.web.authorized.service.CarReplaceService;
import com.koron.car.web.carledger.bean.vo.CarVO;
import com.koron.car.web.carledger.mapper.CarMapper;
import com.koron.car.web.common.CommonUtils;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.util.CodeTools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * (CarReplace)表服务实现类
 *
 * @author makejava
 * @since 2022-04-06 16:57:30
 */
@Service
public class CarReplaceServiceImpl implements CarReplaceService {

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    @TaskAnnotation("queryById")
    public CarReplace queryById(SessionFactory factory, String id) {
        CarReplaceMapper mapper = factory.getMapper(CarReplaceMapper.class);
        return mapper.queryById(id);
    }

    /**
     * 分页查询
     * @param query 筛选条件
     * @return 查询结果
     */
    @Override
    @TaskAnnotation("queryList")
    public List<ReplaceDelimitVO> queryList(SessionFactory factory, ReplaceDelimitQuery query) {
        CarReplaceMapper mapper = factory.getMapper(CarReplaceMapper.class);
        return mapper.queryList(query);
    }

    /**
     * 新增数据
     * @param replace 实例对象
     * @return 实例对象
     */
    @Override
    @TaskAnnotation("insert")
    public CarReplace insert(SessionFactory factory, CarReplace replace) {
        CarReplaceMapper mapper = factory.getMapper(CarReplaceMapper.class);
        CarDelimitMapper delimitMapper = factory.getMapper(CarDelimitMapper.class);
        CarDelimit delimit = delimitMapper.getByNumber(replace.getPrepareNumber());
        Assert.notNull(delimit, "定编号不存在");
        // 同事只能有一个更购申请
        Assert.isTrue(!StringUtils.equals(delimit.getState(), Constants.DelimitState.REPLACEMENT), "更购操作尚未完成");
        replace.setState(Constants.CarReplaceState.DRAFT);
        EamUser eamUser = ThreadLocalContext.get();
        CommonUtils.update(replace, eamUser);
        replace.setDeptId(eamUser.getOrgId());
        replace.setId(CodeTools.getCode32());
        // 检测是否合法
        checkReplace(factory, replace);
        mapper.insert(replace);
        // 更新定编号的状态
        delimit.setState(Constants.DelimitState.REPLACEMENT);
        delimitMapper.update(delimit);
        return replace;
    }

    /**
     * 修改数据
     *
     * @param replace 实例对象
     * @return 实例对象
     */
    @Override
    @TaskAnnotation("update")
    public CarReplace update(SessionFactory factory, CarReplace replace) {
        CarReplaceMapper mapper = factory.getMapper(CarReplaceMapper.class);
        EamUser eamUser = ThreadLocalContext.get();
        CommonUtils.update(replace, eamUser);
        // 检测是否合法
        checkReplace(factory, replace);
        mapper.update(replace);
        return queryById(factory, replace.getId());
    }

    @Override
    @TaskAnnotation("saveOrUpdate")
    public CarReplace saveOrUpdate(SessionFactory factory, CarReplace replace) {
        return StringUtils.isBlank(replace.getId()) ? insert(factory, replace) : update(factory, replace);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        CarReplaceMapper mapper = factory.getMapper(CarReplaceMapper.class);
        return mapper.deleteById(id) > 0;
    }

    @Override
    @TaskAnnotation("workflowUpdate")
    public Boolean workflowUpdate(SessionFactory factory, RedirectQuery redirectQuery) {
        Integer operation = redirectQuery.getOperation();
        CarReplaceVO vo = JSON.parseObject(redirectQuery.getFormVariables(), CarReplaceVO.class);
        CarReplaceMapper mapper = factory.getMapper(CarReplaceMapper.class);
        switch (operation) {
            case 1: // 驳回
                // 驳回的节点编号
                vo.setState(Constants.CarReplaceState.DRAFT);
                break;
            case 32: // 同意
                vo.setState(Constants.CarReplaceState.WAIT_APPROVE);
                break;
            default:
                break;
        }
        // 更新更购单的状态
        CarReplace replace = new CarReplace();
        replace.setId(vo.getId());
        replace.setState(vo.getState());
        mapper.update(replace);
        return true;
    }

    @Override
    @TaskAnnotation("auditFinish")
    public CarReplaceVO auditFinish(SessionFactory factory, CarReplaceVO detail) {
        //1、更新更购申请的状态
        CarReplaceMapper mapper = factory.getMapper(CarReplaceMapper.class);
        detail.setState(Constants.CarReplaceState.FINISH);
        CarReplace replace = new CarReplace();
        replace.setId(detail.getId());
        replace.setState(detail.getState());
        mapper.update(replace);
        // 2、设置旧车编制情况
        CarMapper carMapper = factory.getMapper(CarMapper.class);
        CarVO oldCar = carMapper.queryById(detail.getOldCarId());
        oldCar.setPrepareNumber(null);
        oldCar.setPrepareType(null);
        carMapper.saveOrUpdate(oldCar);
        // 3、设置新车编制情况
        CarVO newCar = carMapper.queryById(detail.getNewCarId());
        newCar.setPrepareNumber(detail.getPrepareNumber());
        newCar.setPrepareType(detail.getPrepareType());
        carMapper.saveOrUpdate(newCar);
        // 4、修改定编号的状态
        CarDelimitMapper delimitMapper = factory.getMapper(CarDelimitMapper.class);
        CarDelimit delimit = delimitMapper.getByNumber(detail.getPrepareNumber());
        delimit.setState(Constants.DelimitState.BINDING);
        delimitMapper.update(delimit);
        return detail;
    }

    public void checkReplace(SessionFactory factory, CarReplace replace) {
        Assert.notNull(replace, "更购申请不能为空");
        Assert.hasText(replace.getOldCarId(), "旧车id不能为空");
        Assert.hasText(replace.getNewCarId(), "新车id不能为空");
        CarMapper mapper = factory.getMapper(CarMapper.class);
        // 1、旧车必须已经绑定了定编号
        CarVO oldCar = mapper.queryById(replace.getOldCarId());
        Assert.hasText(oldCar.getPrepareNumber(), "旧车尚未绑定定编号");
        // 2、新车必须没有绑定定编号
        CarVO newCar = mapper.queryById(replace.getNewCarId());
        Assert.isTrue(StringUtils.isBlank(newCar.getPrepareNumber()), "新车已绑定定编号");
        // 3、检测定编号是否合法
        CarDelimitMapper delimitMapper = factory.getMapper(CarDelimitMapper.class);
        CarDelimit delimit = delimitMapper.getByNumber(replace.getPrepareNumber());
        delimit.setState(Constants.DelimitState.BINDING);
        Assert.notNull(delimit, "定编号不存在");
    }
}
